using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using TaugeLibrary.Classes;
using TaugeLibrary.Interfaces;
using System.Windows.Forms;
using System.Data;
using TaugeLibrary.Classes.Attributes;
using System.Reflection;
using TaugeLibrary.UserControls;
using System.Collections;
using TaugeLibrary.Enums;

namespace TaugeLibrary.Classes.Abstracts
{

    public class SQLOrderItem
    {
        string m_field;
        string m_direction;

        public string FieldName { get { return m_field; } set { m_field = value; } }
        public string Direction { get { if(m_direction!="DESC") 
                                            return "ASC"; 
                                        else 
                                            return "DESC"; 
                                        } 
                                    set { m_direction = value; } }


        public SQLOrderItem(string p_field)
        {
            int n = p_field.Trim().LastIndexOf(' ');
            if (n > 0)
            {
                FieldName = p_field.Trim().Substring(0, n).Trim();
                Direction = p_field.Trim().Substring(n).Trim();
            }
            else
            {
                this.FieldName = p_field;
                this.Direction = "ASC";
            }

                
        }

        public SQLOrderItem(string p_field, string p_direction)
        {
            m_field = p_field; m_direction = p_direction;
        }


    }

    public class SQLOrderManager
    {


        List<SQLOrderItem> m_items = new List<SQLOrderItem>();

        public SQLOrderManager() { m_items.Clear(); }

        public void AddOrder(SQLOrderItem p_item) 
        { 
            foreach(SQLOrderItem i in m_items)
            {
                if (i.FieldName == p_item.FieldName)
                {

                    break;
                }
            }
        }

        public void AddOrder(string p_str) 
        { 

        }
        public void AddOrder(string p_field,string p_direction)
        {

        }
        
    }

    
    [AttBuMgr(
            UnitName = "ABSTRACT",
            //SearchBox = "HR.SearchPanels.SearchABSTRACT",
            //SearchFields = new string[] { "ABSTRACTCode", "Account Group Code", "Status" },
            //SearchDescriptions = new string[] { "ABSTRACTDesc", "Description", "Is Active" },
            //SearchTypes = new eFieldType[] { eFieldType.StringType, eFieldType.StringType, eFieldType.BooleanType },
            //ListSql = "SELECT * FROM ABSTRACT WHERE 1<>2 ",
            Unit = "GL.GL.Classes.ABusinessUnit",
            //UnitForm = "GL.GL.Forms.frmABSTRACT",
            ListHiddenColumns = new string[] { "Col1", "Col5" },
           UnitUI = "TaugeFramework.Forms.frmABSTRACT",
           DBType = LibConstants.DBDATA,
            DllFile = "TaugeFramework.exe"
        )
    ]
    public abstract class ABusinessUnitManager
    {


        private ITaugeFramework m_framework;
        private AttBuMgr m_attbumgr;
        

        #region for sql
        private List<string> m_curr_orders=new List<string>();
        private List<string> m_curr_conditions = new List<string>();
        private string m_fixed_sql;
        private string m_fixed_order;
        
        private bool m_is_where_exists=false;
        private bool m_is_order_changed = false;
        private bool m_is_condition_changed = false;

        #endregion


        public ABusinessUnitManager(ITaugeFramework p_framework)
        {
            m_framework = p_framework;
            m_attbumgr = (AttBuMgr)LibConstants.getAttribute(this.GetType(), typeof(AttBuMgr));

            m_curr_orders.Clear();
            if(m_attbumgr.ListOrders!=null)
                foreach(string s in m_attbumgr.ListOrders)
                    m_curr_orders.Add(s);
            m_is_order_changed = true;


            m_curr_conditions.Clear();
            if (m_attbumgr.ListConditions != null)
                foreach (string s in m_attbumgr.ListConditions)
                    m_curr_conditions.Add(s);
            m_is_condition_changed = true;



            m_fixed_sql = "";
            bool _first = true;
            foreach (string s in m_attbumgr.ListColumns)
            {
                if (_first)
                    _first = false;
                else
                    m_fixed_sql += ", ";
                m_fixed_sql += s;
            }


            m_fixed_sql += " FROM ";
            foreach (string s in m_attbumgr.ListTables)
                m_fixed_sql += " " + s + " ";

            m_is_where_exists = false;
            if (m_attbumgr.ListFixedCondition != "" && m_attbumgr.ListFixedCondition != null)
            {
                m_is_where_exists = true;
                m_fixed_sql += " WHERE " + m_attbumgr.ListFixedCondition;
            }

        }

        #region core
        public virtual string getUnitName()
        {
            if (m_attbumgr == null)
                return "NOT IMPLEMENT GETUNITNAME";
            else
                return m_attbumgr.UnitName;
        }

        public virtual string getImageRes()
        {
            if (m_attbumgr == null)
                return "";
            else
                return m_attbumgr.ImageRes;
        }

        public virtual ABusinessUnit createUnit()
        {
            if (m_attbumgr == null)
                return null;
            else
                try
                {
                    Assembly _assembly = Assembly.LoadFrom(m_attbumgr.DllFile);
                    return (ABusinessUnit)Activator.CreateInstance(_assembly.GetType(m_attbumgr.Unit), m_framework);

                }
                catch
                {
                    return null;
                }

        }

        public virtual ABusinessUnitUI createUnitUI()
        {
            if (m_attbumgr == null)
            { return null; }
            else
            {
                try
                {
                    Assembly _assembly = Assembly.LoadFrom(m_attbumgr.DllFile);
                    return (ABusinessUnitUI)Activator.CreateInstance(_assembly.GetType(m_attbumgr.UnitUI), m_framework, createUnit());

                }
                catch
                {
                    return null;
                }
            }

        }
        #endregion

        #region for list

        public virtual string[] getListHiddenColumn()
        {            
            return m_attbumgr.ListHiddenColumns;
        }
        public virtual string getListSql()
        {
            return getListSql(false,false);
        }
        public virtual string getOrder(bool p_reverse)
        {
            
            if (m_is_order_changed || p_reverse)
            {
                string _result = " ORDER BY ";
                if (m_curr_orders.Count > 0)
                {
                    foreach (string s in m_curr_orders)
                    {
                        string[] astr = s.Split(' ');
                        if (astr[astr.Length - 1] == "DESC")
                        {
                            if (p_reverse)
                                _result += astr[0] + " ASC ";
                            else
                                _result += astr[0] + " DESC ";
                        }
                        else if (astr[astr.Length - 1] == "ASC")
                        {
                            if (p_reverse)
                                _result += astr[0] + " DESC ";
                            else
                                _result += astr[0] + " ASC ";
                        }
                        else
                        {
                            if (p_reverse)
                                _result += s + " DESC ";
                            else
                                _result += s + " ASC ";
                        }

                    }
                    
                }
                else
                {
                    //analyze first column
                    int _idx = m_attbumgr.ListColumns[0].IndexOf(' ');
                    string stmp = m_attbumgr.ListColumns[0];
                    if (_idx > 0)
                        stmp = stmp.Substring(0, _idx + 1);

                    if (p_reverse)
                        _result += stmp + " DESC ";
                    else
                        _result += stmp + " ASC ";
                }
                if (!p_reverse)
                    m_fixed_order = _result;
                return _result;
            }
            else
            {
                return m_fixed_order;
            }

            
        }
        public virtual string getListSql(bool p_top_1,bool p_last_1)
        {
            if (m_attbumgr == null)
                return "NOT IMPLEMENT GETLISTSQL";
            else
            {
                string _sql = "SELECT ";
                
                if (p_top_1||p_last_1)
                    _sql += " TOP 1 ";

                if (m_attbumgr.ListIsDistinct)
                    _sql += " DISTINCT ";

                _sql += m_fixed_sql + getOrder(p_last_1);
                
                return _sql;
            }
        }
        public virtual string getListSql(string p_csid)
        {
            return getListSql().Replace("@CSID", LibConstants.sqlStr(p_csid));
        }

        #endregion

        public virtual string getDBType()
        {
            if (m_attbumgr == null)
                return LibConstants.DBDATA;
            else
                return m_attbumgr.DBType;
            
            
        }

        public virtual string getLastRecordSql()
        {
            return getListSql(true,true);
        }


        public virtual ABusinessUnit GetUnitFromList(DataRow p_sdr)
        {
            string _sql = GetSQLUnitFromList(p_sdr);

            if (_sql == "")
                return null;

            ABusinessUnit x = createUnit();
            if (x.doRead(_sql))
                return x;
            else
                return null;


        }

        public ABusinessUnit getNext(ABusinessUnit m_current_unit)
        {
            return null;
        }


        protected virtual string GetSQLUnitFromList(DataRow p_sdr)
        {

            ABusinessUnit x = createUnit();
            
            AttTable _tbl = (AttTable)LibConstants.getAttribute(x.GetType(), typeof(AttTable));
            if (_tbl != null)
            {
                StringBuilder _sql = new StringBuilder("SELECT TOP 1 * FROM " + _tbl.TableName);
                bool _bfirst = true;
                AttPrimaryKey _pk = (AttPrimaryKey)LibConstants.getAttribute(x.GetType(), typeof(AttPrimaryKey));
                if (_pk != null)
                {                    
                    foreach (string s in _pk.PrimaryKeys)
                    {
                        foreach (PropertyInfo pi in x.GetType().GetProperties())
                        {
                            AttField _fld = (AttField)LibConstants.getAttribute(pi, typeof(AttField));
                            if (_fld != null)
                            {
                                if (s == _fld.FieldName)
                                {
                                    if (_bfirst)
                                    {
                                        _sql.Append(" WHERE ");
                                        _bfirst = false;
                                    }
                                    else
                                    {
                                        _sql.Append(" AND ");
                                    }

                                    _sql.Append(_fld.FieldName + " = ");
                                    _sql.Append(LibConstants.toSql(p_sdr[_fld.Alias]));
                                    break;
                                }
                            }
                        }

                        
                    }
                }
                return _sql.ToString();                
            }
            else
            {
                return "";
            }
            

        }
/*
        public virtual string getPeriodeCondition(DateTime dateTime)
        {
            return "";
        }

        */


    }


    [
        AttBu(
             TableName = "ABSTRACT",
             PrimaryKeys = new string[] { "ABSTRACTCode" },
             DataPKeys = new string[] { "AbstractCode" }, //or DataPSets = new string[] { "default" }, to call from attatr
             QuerySql = "SELECT TOP 1 * FROM ABSTRACT",
               DBType = LibConstants.DBDATA
        )
    ]
    [AttArr(ArrName = "default", StrItems = new string[] { "AbstractCode" })] //if use datapsets
    public abstract class ABusinessUnit
    {
        #region memvar
        protected int m_createuser;
        protected int m_updateuser;
        protected DateTime m_createtime;
        
        protected DateTime m_updatetime;
        protected string m_errormessage;
        private AttBu m_attbu;
        protected ITaugeFramework m_framework;
        #endregion

        #region properties
        
        [AttBindControl(eControlType.LABEL, "lblCreateUser",WriteOnly=true,BindParentForm = true)]
        [AttField("CreateUser", eDBDataType.USERSTAMPCREATE, 0)]
        public int CreateUser { get { return m_createuser; } set { m_createuser = value; } }

        [AttBindControl(eControlType.LABEL, "lblUpdateUser", WriteOnly = true, BindParentForm = true)]
        [AttField("UpdateUser", eDBDataType.USERSTAMP, 0)]
        public int UpdateUser { get { return m_updateuser; } set { m_updateuser = value; } }

        [AttBindControl(eControlType.LABEL, "lblCreateTime", WriteOnly = true, BindParentForm = true)]
        [AttField("CreateTime", eDBDataType.TIMESTAMPCREATE, 0)]
        public DateTime CreateTime { get { return m_createtime; } set { m_createtime = value; } }

        [AttBindControl(eControlType.LABEL, "lblUpdateTime", WriteOnly = true, BindParentForm = true)]
        [AttField("UpdateTime", eDBDataType.TIMESTAMP, 0)]
        public DateTime UpdateTime { get { return m_updatetime; } set { m_updatetime = value; } }
        
        public string ErrorMessage { get { return m_errormessage; } }
        //public Database DB { get { return m_db; } }
        public AttBu Attribute { get { return m_attbu; } }
        #endregion


        public ABusinessUnit(ITaugeFramework p_framework)
        {
            m_framework = p_framework;
        }


        #region virtual
        
        public virtual TaugeCommand[] getActionCommand(string p_action) 
        {
            AttTable _tbl = (AttTable)LibConstants.getAttribute(this.GetType(), typeof(AttTable));
            if (_tbl != null)
            {
                List<TaugeCommand> _ax = new List<TaugeCommand>();
                if (p_action == LibConstants.eINSERT)
                {
                    //check exists pk                
                    _ax.Add(new TaugeCommand(GetSQLRead(), TaugeCommand.eCOMMANDTYPE.ERROR_ON_EXISTS, "data sudah ada"));

                    //check exists unique
                    string[] _uniqesSQL = GetSQLReadKeys();
                    if (_uniqesSQL != null)
                        foreach (string s in _uniqesSQL)
                            _ax.Add(new TaugeCommand(s, TaugeCommand.eCOMMANDTYPE.ERROR_ON_EXISTS, "data sudah ada"));

                    //check exists foreign key
                    string[] _foreignSQL = getSQLCheckForeignKeys();
                    if (_foreignSQL != null)
                        foreach (string s in _foreignSQL)
                            _ax.Add(new TaugeCommand(s, TaugeCommand.eCOMMANDTYPE.ERROR_ON_NOT_EXISTS, "data tidak di temukan"));

                    //insert command
                    StringBuilder _insertSQL1 = new StringBuilder("INSERT INTO " + _tbl.TableName + " ( " );
                    StringBuilder _insertSQL2 = new StringBuilder(" VALUES ( " );
                    bool _bfirst = true;
                    PropertyInfo piIdentity = null;
                    foreach (PropertyInfo pi in this.GetType().GetProperties())
                    {
                        AttField _fld = (AttField)LibConstants.getAttribute(pi, typeof(AttField));
                        if (_fld != null)
                        {
                            if (_fld.FieldType != eDBDataType.IDENTITY)
                            {
                                if (_bfirst)
                                {
                                    _bfirst = false;
                                }
                                else
                                {
                                    _insertSQL1.Append(" , ");
                                    _insertSQL2.Append(" , ");
                                }
                                _insertSQL1.Append(_fld.FieldName);
                                if (_fld.FieldType == eDBDataType.TIMESTAMP || _fld.FieldType == eDBDataType.TIMESTAMPCREATE)
                                    _insertSQL2.Append(@"GETDATE()");
                                else if (_fld.FieldType == eDBDataType.USERSTAMP || _fld.FieldType == eDBDataType.USERSTAMPCREATE)
                                    _insertSQL2.Append(m_framework.getActiveUserId().ToString());
                                else
                                    _insertSQL2.Append(LibConstants.toSql(pi.GetValue(this, null)));
                            }
                            else if (_fld.FieldType == eDBDataType.IDENTITY)
                            {
                                piIdentity = pi;
                            }
                        }
                    }
                    _insertSQL1.Append(")");
                    _insertSQL2.Append(")");
                    _ax.Add(new TaugeCommand(_insertSQL1.ToString() + _insertSQL2.ToString(), TaugeCommand.eCOMMANDTYPE.EXECUTE_NON_QUERY, ""));

                    if (piIdentity != null) 
                        _ax.Add(new TaugeCommand("SELECT @@identity",TaugeCommand.eCOMMANDTYPE.RETREIVE_IDENTITY,piIdentity));

                }
                else if (p_action == LibConstants.eUPDATE)
                {
                    //check exists pk                
                    _ax.Add(new TaugeCommand(GetSQLRead(), TaugeCommand.eCOMMANDTYPE.ERROR_ON_NOT_EXISTS, "data tidak ditemukan"));

                    //check exists unique
                    string[] _uniqesSQL = GetSQLReadKeys(true);
                    if (_uniqesSQL != null)
                        foreach (string s in _uniqesSQL)
                            _ax.Add(new TaugeCommand(s, TaugeCommand.eCOMMANDTYPE.ERROR_ON_EXISTS, "data sudah ada"));

                    //check exists foreign key
                    string[] _foreignSQL = getSQLCheckForeignKeys();
                    if (_foreignSQL != null)
                        foreach (string s in _foreignSQL)
                            _ax.Add(new TaugeCommand(s, TaugeCommand.eCOMMANDTYPE.ERROR_ON_NOT_EXISTS, "data tidak di temukan"));

                    //update command
                    StringBuilder _updateSQL = new StringBuilder("UPDATE " + _tbl.TableName + " SET ");                    
                    bool _bfirst = true;
                    foreach (PropertyInfo pi in this.GetType().GetProperties())
                    {
                        AttField _fld = (AttField)LibConstants.getAttribute(pi, typeof(AttField));
                        if (_fld != null && _fld.FieldType != eDBDataType.IDENTITY && _fld.FieldType !=eDBDataType.TIMESTAMPCREATE && _fld.FieldType!=eDBDataType.USERSTAMPCREATE)
                        {
                            if (_bfirst)
                            {
                                _bfirst = false;
                            }
                            else
                            {
                                _updateSQL.Append(" , ");                                
                            }
                            _updateSQL.Append(_fld.FieldName + " = ");
                            if (_fld.FieldType == eDBDataType.TIMESTAMP || _fld.FieldType == eDBDataType.TIMESTAMPCREATE)
                                _updateSQL.Append(@"GETDATE()");
                            else if (_fld.FieldType == eDBDataType.USERSTAMP || _fld.FieldType == eDBDataType.USERSTAMPCREATE)
                                _updateSQL.Append(m_framework.getActiveUserId().ToString());
                            else
                                _updateSQL.Append(LibConstants.toSql(pi.GetValue(this, null)));
                        }
                    }
                    _updateSQL.Append(GetPrimaryCondition());
                    _ax.Add(new TaugeCommand(_updateSQL.ToString() , TaugeCommand.eCOMMANDTYPE.EXECUTE_NON_QUERY, ""));
                }
                else if (p_action == LibConstants.eDELETE)
                {
                    //check exists pk                
                    _ax.Add(new TaugeCommand(GetSQLRead(), TaugeCommand.eCOMMANDTYPE.ERROR_ON_NOT_EXISTS, "data tidak ditemukan"));

                    //delete command
                    _ax.Add(new TaugeCommand("DELETE " + _tbl.TableName + GetPrimaryCondition(),TaugeCommand.eCOMMANDTYPE.EXECUTE_NON_QUERY,""));                    

                }

                return _ax.ToArray();
            }
            return null; 
        }

        public virtual string[] getSQLCheckForeignKeys()
        {
            AttForeignKey[] _fks = (AttForeignKey[])LibConstants.getAttributes(this.GetType(), typeof(AttForeignKey));
            if (_fks != null)
            {
                List<string> _aresult=new List<string>();
                foreach (AttForeignKey _fk in _fks)
                {
                    StringBuilder _sql = new StringBuilder("SELECT TOP 1 * FROM " + _fk.TableReference);
                    bool _bfirst = true;
                    foreach (string s in _fk.ForeignKeys)
                    {
                        foreach (PropertyInfo pi in this.GetType().GetProperties())
                        {
                            AttField _fld = (AttField)LibConstants.getAttribute(pi, typeof(AttField));
                            if (_fld != null)
                            {
                                if (_fld.FieldName == s)
                                {
                                    _sql.Append(" WHERE ");
                                    _bfirst = false;
                                }
                                else
                                    _sql.Append(" AND ");

                                _sql.Append(_fld.FieldName + " = " + LibConstants.toSql(pi.GetValue(this, null)));
                            }
                        }
                    }
                    _aresult.Add(_sql.ToString());

                }
                return _aresult.ToArray();
            }
            return null;
        }

        public virtual string GetPrimaryCondition(bool p_reverse, bool p_with_where)
        {
            StringBuilder _result = new StringBuilder();

            bool _bfirst = true;

            AttPrimaryKey _pk = (AttPrimaryKey)LibConstants.getAttribute(this.GetType(), typeof(AttPrimaryKey));
            if (_pk != null)
            {
                foreach (string s in _pk.PrimaryKeys)
                {

                    foreach (PropertyInfo pi in this.GetType().GetProperties())
                    {
                        AttField _fld = (AttField)LibConstants.getAttribute(pi, typeof(AttField));
                        if (_fld != null)
                        {
                            if (s == _fld.FieldName)
                            {
                                if (_bfirst)
                                {
                                    if(p_with_where)
                                        _result.Append(" WHERE ");
                                    if (p_reverse)
                                        _result.Append(" NOT ");
                                    _result.Append(" ( ");
                                    _bfirst = false;
                                }
                                else
                                    _result.Append(" AND ");

                                _result.Append(_fld.FieldName + " = " + LibConstants.toSql(pi.GetValue(this, null)));
                                break;
                            }
                        }
                    }
                }
                if(!_bfirst)
                    _result.Append(" ) ");

            }
            return _result.ToString();
        }

        public virtual string GetPrimaryCondition()
        {
            return GetPrimaryCondition(false,true);
        }

        public virtual string GetSQLRead()
        {
            AttTable _tbl = (AttTable)LibConstants.getAttribute(this.GetType(), typeof(AttTable));
            if (_tbl != null)
            {
                StringBuilder _result = new StringBuilder("SELECT TOP 1 * FROM " + _tbl.TableName);
                _result.Append(GetPrimaryCondition());
                return _result.ToString();
             
            }
            return "";


        }

        public virtual string[] GetSQLReadKeys()
        {
            return GetSQLReadKeys(false);
        }

        public virtual string[] GetSQLReadKeys(bool p_check_other_id)
        {
            AttTable _tbl = (AttTable)LibConstants.getAttribute(this.GetType(), typeof(AttTable));
            if (_tbl != null)
            {
                

                AttUniqueKey[] _uks = (AttUniqueKey[])LibConstants.getAttributes(this.GetType(), typeof(AttUniqueKey));
                if (_uks != null)
                {

                    List<string> _aresult = new List<string>();
                    foreach (AttUniqueKey _uk in _uks)
                    {
                        StringBuilder _result = new StringBuilder("SELECT TOP 1 * FROM " + _tbl.TableName);
                        bool _bfirst = true;
                        foreach (string s in _uk.UniqueKeys)
                        {
                            foreach (PropertyInfo pi in this.GetType().GetProperties())
                            {
                                AttField _fld = (AttField)LibConstants.getAttribute(pi, typeof(AttField));
                                if (_fld != null)
                                {
                                    if (s == _fld.FieldName)
                                    {
                                        if (_bfirst)
                                        {
                                            _result.Append(" WHERE (");
                                            _bfirst = false;
                                        }
                                        else
                                            _result.Append(" AND ");

                                        _result.Append(_fld.FieldName + "=" + LibConstants.toSql(pi.GetValue(this, null)));
                                        break;
                                    }
                                }
                            }
                        }
                        if (!_bfirst)
                        {
                            _result.Append(" ) ");
                            if (p_check_other_id)
                            {
                                _result.Append(" AND " + GetPrimaryCondition(true, false));
                            }
                        }


                        _aresult.Add(_result.ToString());
                    }
                    if(_aresult.Count>0)
                        return _aresult.ToArray();
                }
                
            }
            return null;


        }

        #endregion

        public bool doRead()
        {
            return doRead(GetSQLRead());            
        }

        public bool doReadKey()
        {
            string [] astr=GetSQLReadKeys();
            if (astr == null)
                return doRead();
            else
                return doRead(astr[0]);
        }

        


        public bool doRead(string p_sql)
        {
            return doRead(new SqlCommand(p_sql)); //doRead(new SqlCommand(p_sql));
        }

        public virtual bool doRead(SqlCommand p_cmd)
        {

            try
            {
                DataTable _dt = m_framework.executeDataTable(getDBType(), p_cmd);
                if (_dt.Rows.Count > 0)
                {
                    return FillData(_dt.Rows[0]);                    
                }
                else
                {
                    m_errormessage = "data tidak ditemukan";
                    return false;
                }                
            }
            catch(Exception ex)
            {
                m_errormessage = ex.Message;
                return false;
            }
         
        }

        public virtual string getDBType()
        {
            return LibConstants.DBDATA;
        }

        public virtual bool doAction(string p_action)
        {
            
            SqlTransaction st = null;
            try
            {
                TaugeCommand[] acmd = getActionCommand(p_action);
                PropertyInfo piIdentity = null;
                TaugeCommand tcIdentity = null;
                foreach(TaugeCommand tc in acmd)
                {
                    if (tc.Type == TaugeCommand.eCOMMANDTYPE.RETREIVE_IDENTITY)
                    {
                        tcIdentity = tc;
                        piIdentity = (PropertyInfo)tc.Extension;
                        break;
                    }
                }
                m_framework.executeTaugeCommand(getDBType(),acmd);

                if(piIdentity!=null)
                    piIdentity.SetValue(this, Convert.ToInt32(tcIdentity.Extension), null);
                return true;
            }
            catch (Exception ex)
            {
                m_errormessage = ex.Message;
                //try
                //{
                //    st.Rollback();
                //}
                // catch(Exception exb) { }
                return false;
            }
            
            return false;
            
            return false;
        }


        public virtual bool FillData(DataRow p_sdr)
        {
            try
            {
                foreach (PropertyInfo pi in this.GetType().GetProperties())
                {
                    AttField af = (AttField)LibConstants.getAttribute(pi, typeof(AttField));
                    if (af != null)
                        pi.SetValue(this, p_sdr[af.FieldName], null);
                }
                return true;
            }
            catch (Exception ex)
            {
                m_errormessage = ex.Message;
                return false;
            }
        }

        


    }
}
